package com.bdqn.zhang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bdqn.zhang.config.WebSocket;
import com.bdqn.zhang.pojo.Message;
import com.bdqn.zhang.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <Description> <br>
 *
 * @author tao<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2019/09/26 16:26 <br>
 * @see com.bdqn.zhang.controller <br>
 */
@Controller
public class LoginController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private WebSocket webSocket;


    @RequestMapping(value="/index")
    public String toLogin(){
        return "index";
    }


    @RequestMapping(value="/count")
    @ResponseBody
    public String getCount(){
        return JSONArray.toJSONString(messageService.count());
    }

    @RequestMapping(value="/toadd")
    public String toAdd(){
        return "add";
    }

    @RequestMapping(value="/addSave")
    @ResponseBody
    public String addMsg(Message msg){
        Long number = messageService.addMsg(msg);
        if(number > 0){
            webSocket.sendMessage(messageService.count().toString());
        }
        return "success";
    }
}
